<?php
/**
 * empleados
 *
 * empleados_model.php
 * @author Hector Ruiz
 * @version 1.0
 * @copyright Hector Ruiz, 13 Noviembre, 2011
 * @package 1.0
 **/

class Empleados_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
    }

    function read($table, $id_name, $id) {
        $this->db->where($id_name, $id);
        $query = $this->db->get($table);

        return $query->result_array();
    }
    
    function read_w($table, $where) {
        foreach($where as $k => $v) {
            $this->db->where($k, $v);
        }
        $query = $this->db->get($table);

        return $query;
    }

    function delete($table, $id_name, $id) {
        $result = 0;
        $result = $this->db->delete($table, array($id_name => $id));

        return $result;
    }

    function update($table, $id_name, $id, $data) {
        $result = 0;
        $result = $this->db->update($table, $data, array($id_name => $id));

        return $result;
    }

    function create($table, $data) {
        $id = 0;
        $this->db->insert($table, $data);
        $id = $this->db->insert_id();

        return $id;
    }

    function exists($table, $id_name, $id) {
        $exists = 0;
        $this->db->where($id_name, $id);
        $query = $this->db->get($table);
        if($query->num_rows() > 0) {
            $exists = 1;
        }

        return $exists;
    }

    function save($table, $id_name, $id, $data) {
        $exists = $this->exists($table, $id_name, $id);
        if($exists) {
            $result = $this->db->update($table, $data, array($id_name => $id));
        } else {
            $result = $this->db->insert($table, $data);
            $id = $this->db->insert_id();
        }

        return $id;
    }

    function get_list($table = "", $where = "", $order = "", $limit = "") {
        if($where) {
            if(is_array($where)) {
                if(!empty($where)) {
                    foreach($where as $k => $v) {
                        $this->db->where($k, $v);
                    }
                }
            }
        }
        if($order) {
            if(is_array($order)) {
                if(!empty($order)) {
                    foreach($order as $v) {
                        $this->db->order_by($v);
                    }
                }
            } else {
                $this->db->order_by($order);
            }
        }
        if($limit) {
            $tmp = $limit;
            $tmp = explode(',', $tmp);
            (count($tmp) > 1) ? $this->db->limit($tmp[0], $tmp[1]) : $this->db->limit($tmp[0]);
        }
        $query = $this->db->get($table);

        return $query->result_array();
    }

    function fields($table) {
        $fields = array();
        switch($table) {
            case $table:
                $fields['region'] = "Region:";
                $fields['stvc'] = "STVC:";
                $fields['nombre_1'] = "<font color='red'>Primer Nombre</font>:";
                $fields['nombre_2'] = "Segundo Nombre:";
                $fields['apellido_paterno'] = "<font color='red'>Apellido Paterno</font>:";
                $fields['apellido_materno'] = "Apellido Materno:";
                $fields['usuario_red'] = "<font color='red'>Usuario de RED</font>:";
                $fields['departamento'] = "Departamento:";
                $fields['puesto'] = "Puesto:";
                $fields['extension'] = "Extension:";
            break;
        }

        return $fields;
    }

    function rules($table, $fields) {
        switch($table) {
            case $table:
                $this->form_validation->set_rules('stvc', $fields['stvc'], 'required|is_natural_no_zero');
                $this->form_validation->set_rules('nombre_1', $fields['nombre_1'], 'required');
                $this->form_validation->set_rules('apellido_paterno', $fields['apellido_paterno'], 'required');
                $this->form_validation->set_rules('usuario_red', $fields['usuario_red'], 'required|is_unique["empleados"."usuario_red"]');
                $this->form_validation->set_rules('departamento', $fields['departamento'], 'required|is_natural_no_zero');
                $this->form_validation->set_rules('puesto', $fields['puesto'], 'required|is_natural_no_zero');
            break;
        }
    }

    function format_date($date, $sw = 0) {
        //sw = 0 [external->internal], sw = 1 [internal->external]
        $return_date = 0;
        if($date) {
            $tmp = explode('-', $date);
            if(is_array($tmp)) {
                if(count($tmp) == 3) {
                    if($sw) {
                        list($year, $month, $day) = $tmp;
                        $return_date = $month. '-' . $day . '-' .$year;
                    } else {
                        list($month, $day, $year) = $tmp;
                        $return_date = $year.'-'.$month.'-'.$day;
                    }
                } else {
                    $return_date = NULL;
                }
            } else {
                $return_date = NULL;
            }
        } else {
            $return_date = NULL;
        }

        return $return_date;
    }                            
}
/* end of file */
